/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gidoo.owl2.viewer.panels;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.gidoo.owl2.backend.DatabaseError;
import org.gidoo.owl2.backend.DictionaryProvider;
import org.gidoo.owl2.viewer.SearchPage;
import org.gidoo.owl2.viewer.session.OwlSession;

/**
 *
 * @author thomas
 */
public class SearchPanel extends Panel
{

  private DictionaryProvider dicProvider;
  private DropDownChoice<Long> dcDictionaries;

  public SearchPanel(String wicketID)
  {
    this(wicketID, -1);
  }

  public SearchPanel(String wicketID, long selectedDocID)
  {
    super(wicketID);

    this.dicProvider = ((OwlSession) getSession()).getDicProvider();

    Form form = new Form("form");
    add(form);

    List<Long> docs = new LinkedList<Long>();
    try
    {
      docs = dicProvider.getDocuments();
    }
    catch(DatabaseError ex)
    {
      error("Database error: " + ex.getMessage());
    }

    Long listElementToSelect = null;

    if(selectedDocID != -1)
    {
      for(int i = 0; i < docs.size(); i++)
      {
        if(docs.get(i).equals(selectedDocID))
        {
          listElementToSelect = docs.get(i);
          break;
        }
      }
    }
    else if(docs.size() > 0)
    {
      listElementToSelect = docs.get(0);
    }

    dcDictionaries = new DropDownChoice("dcDictionaries",
      new Model<Long>(listElementToSelect), docs,
      new ChoiceRenderer<Long>()
      {

        @Override
        public Object getDisplayValue(Long object)
        {
          try
          {
            return dicProvider.getDocumentNameFromID(object);
          }
          catch(DatabaseError ex)
          {
            error("Database error: " + ex.getMessage());
          }
          return object.toString();
        }
      });

    dcDictionaries.add(new AjaxFormSubmitBehavior("onchange") {

      @Override
      protected void onSubmit(AjaxRequestTarget arg0)
      {
      }

      @Override
      protected void onError(AjaxRequestTarget arg0)
      {
      }
    });

    form.add(dcDictionaries);

    final AutoCompleteTextField<String> txtSearch =
      new AutoCompleteTextField<String>("txtSearch", new Model<String>(""))
      {

        @Override
        protected Iterator<String> getChoices(String s)
        {

          if(s.length() >= 3)
          {
            List<String> result = new LinkedList<String>();
            try
            {
              if(dcDictionaries.getModelObject() != null)
              {
                result = dicProvider.getMatchingSearchTerms(dcDictionaries.getModelObject(),
                  s);
              }
            }
            catch(DatabaseError ex)
            {
              error("Database error: " + ex.getMessage());
            }
            if(result.size() == 0)
            {
              result.add("..::no matching entries::..");
            }
            return result.iterator();
          }
          else
          {
            List<String> result = new LinkedList<String>();
            result.add("..::not enough input:..");
            return result.iterator();
          }
        }
      };
    form.add(txtSearch);

    Button btSearch = new Button("btSearch", new Model<String>("Search"))
    {

      @Override
      public void onSubmit()
      {
        PageParameters params = new PageParameters();
        params.put("search", txtSearch.getModelObject());
        params.put("id", "" + dcDictionaries.getModelObject());

        setResponsePage(SearchPage.class, params);

      }
    };
    form.add(btSearch);
  }
}
